﻿
namespace System.Windows.Controls
{
    /// <summary>
    /// 忙闲状态扩展，用于子窗体，by zhlin 2011-07-07
    /// </summary>
    public static class BusyIndicatorEmbed
    {
        private const string Name = "busyIndicator";

        public static void SetBusy(this ChildWindow window)
        {
            var grid = (Grid)window.Content;
            if (null == grid)
            {
                return;
            }
            var busyIndicator = (BusyIndicator)grid.FindName(Name);
            if (busyIndicator == null)
            {
                var style = (Style)Application.Current.Resources["BusyIndicatorStyle"];
                busyIndicator = new BusyIndicator();
                busyIndicator.Name = Name;
                Grid.SetRowSpan(busyIndicator, 100);
                Grid.SetColumnSpan(busyIndicator, 100);
                busyIndicator.HorizontalAlignment = HorizontalAlignment.Stretch;
                busyIndicator.VerticalAlignment = VerticalAlignment.Stretch;
                busyIndicator.Style = style;
                grid.Children.Add(busyIndicator);
            }
            window.HasCloseButton = false;
            busyIndicator.IsBusy = true;
        }

        public static void SetVacancy(this ChildWindow window)
        {
            var grid = (Grid)window.Content;
            if (null == grid)
            {
                return;
            }
            var busyIndicator = (BusyIndicator)grid.FindName(Name);
            if (busyIndicator == null)
            {
                var style = (Style)Application.Current.Resources["BusyIndicatorStyle"];
                busyIndicator = new BusyIndicator();
                busyIndicator.Name = Name;
                Grid.SetRowSpan(busyIndicator, 100);
                Grid.SetColumnSpan(busyIndicator, 100);
                busyIndicator.HorizontalAlignment = HorizontalAlignment.Stretch;
                busyIndicator.VerticalAlignment = VerticalAlignment.Stretch;
                busyIndicator.Style = style;
                grid.Children.Add(busyIndicator);
            }
            window.HasCloseButton = true;
            busyIndicator.IsBusy = false;
        }
    }
}
